﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for LoaiCauHoiDAL
/// </summary>
public class LoaiCauHoiDAL
{
    public static object IsNull(object checkValue, object replaceValue)
    {
        if (checkValue.Equals(System.DBNull.Value) || checkValue == null)
            return replaceValue;
        return checkValue;
    }

    public static List<LoaiCauHoiINFO> listAll()
    {
        string sql = "select * from LoaiCauHoi";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetListObjects<LoaiCauHoiINFO>(dr);
    }

    public static LoaiCauHoiINFO GetInfo(int maloai)
    {
        string sql = "select * from LoaiCauHoi where maloai ="+maloai+"";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetObject<LoaiCauHoiINFO>(dr);
    }

    public static int Insert(LoaiCauHoiINFO obj)
    {
        obj.MaLoai = GetMa();
        obj.MaLoai = obj.MaLoai + 1;
        string sql = "INSERT INTO LOAICAUHOI (MaLoai,TenLoai) VALUES ("+obj.MaLoai+",'"+obj.TenLoai+"')";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int Update(LoaiCauHoiINFO obj)
    {
        string sql = "Update LOAICAUHOI set TenLoai='" + obj.TenLoai + "' Where maloai="+obj.MaLoai+"";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int Delete(int maloai)
    {
        string sql = "Delete from LoaiCauHoi where maloai =" + maloai + "";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int GetMa()
    {
        string sql = "SELECT ISNULL(MAX(MaLoai),0) FROM LOAICAUHOI";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        int ma = 0;
        if (dr.Read()) {
            ma = int.Parse(dr[0].ToString());
        }
        dr.Close();
        return ma;
    }
}